import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '../components/HelloWorld.vue'
import User from '../components/user.vue'
import Roles from '../components/power/roles.vue'
import Rights from '../components/power/rights.vue'
import Reports from '../components/reports.vue'
import Params from '../components/goods/Params.vue'
import Orders from '../components/orders.vue'
import GoodsList from '../components/goods/List.vue'
import Cate from '../components/goods/Cate.vue'
import Login from '../views/Login.vue'
import Welcome from '../components/welcome.vue'

Vue.use(Router)


// 设置导航守卫的时候得重构一下路由的结构  这样才有导航守卫的router.beforeEach
const router = new Router({
  routes: [
    // 在这里重定向 如果没带如何路径 就返回登陆界面
    {path: '/',redirect: '/login'},
    {path: '/login',component: Login},
    {
      //登陆中设置了登陆后跳转到/hello 为父组件 这父组件的下面有以下自路由  
      path: '/hello',
      component: HelloWorld,
      // 这里就是登录后 默认的重定向界面
      redirect:'/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/roles', component: Roles},
        { path: '/rights', component: Rights},
        { path: '/reports', component: Reports},
        { path: '/params', component: Params},
        { path: '/orders', component: Orders},
        { path: '/goods', component: GoodsList},
        { path: '/categories', component: Cate},

      ]
    }
  ]
})

router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router